--#include "data\config\boss\DreamLandBossConfig.lua" once
--#include "data\functions\Common\CommonFunc.lua" once              --一些通用方法

--开放入口，创建boss
function DreamLandBossStart()
    local sVar = System.getDyanmicVar()
    sVar.dreamLandBossStart = 1
    DreamLandBossCreateBoss()
    System.broadcastTipmsg(Lang.ScriptTips.DreamLandBoss014,ttScreenCenter + ttChatWindow)
end

function DreamLandBossExchangeTime()
    local sVar = System.getDyanmicVar()
    local time = System.getCurrMiniTime()
    sVar.dreamLandBossExchangeEndTime = time + 300
    local dreamLandBossScene = {DreamLandBossConfig.BossScene, DreamLandBossConfig.EnterPos[1]}
    for i,v in ipairs(dreamLandBossScene) do
        local playerList = LuaHelp.getSceneActorListById(v) or {}
        for i2,v2 in ipairs(playerList) do
            local lastTime = sVar.dreamLandBossExchangeEndTime - time
            local boardStr = Lang.ScriptTips.DreamLandBoss018                --注意：字符串中要带有一个 %d
            OpenBoardPanel( v2, enBoardPanelStyle_CountDown1, boardStr, lastTime )
        end
    end
end

--活动结束，踢出玩家，清除怪物
function DreamLandBossEnd()
    Actor.unregScriptCallback( System.getGlobalNpc(), "DreamLandBossPreSecondTimeFunc")
    
    DreamLandBossKickPlayers()
    DreamLandBossClearBoss()

    local sVar = System.getDyanmicVar()
    sVar.dreamLandBossStart = nil
    sVar.dreamLandBossExchangeStart = nil
    sVar.DreamLandBossId = nil
    sVar.DreamLandBossDeadId = nil
    sVar.DreamLandBossDeadEndTime = nil
    sVar.dreamLandBossExchangeEndTime = nil

    System.broadcastTipmsg(Lang.ScriptTips.DreamLandBoss015,ttScreenCenter + ttChatWindow)
end

--进入
function DreamLandBossEnter(sysarg, reliveType)
    if not IsDreamLandBossStart() then
        return 
    end
    if Actor.getIsDartFlag(sysarg) then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
        return
    end
    if reliveType then
        if not IsDreamLandBoss(sysarg) then
            return
        end
        if reliveType == 2 then
            Actor.enterScene(sysarg,unpack(DreamLandBossConfig.EnterPos))
        end
    else 
        Actor.enterScene(sysarg,unpack(DreamLandBossConfig.EnterPos)) 

        Actor.setPkMode( sysarg, fpGuild )  

        local sVar = System.getDyanmicVar()
        local time = System.getCurrMiniTime()
        if sVar.dreamLandBossExchangeEndTime then
            local lastTime = sVar.dreamLandBossExchangeEndTime - time
            local boardStr = Lang.ScriptTips.DreamLandBoss018                --注意：字符串中要带有一个 %d
            OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, lastTime )
        end

        --每天记录进入幻境BOSS的次数，每天只能进行1次（不支持每天开1次以上）
        local todayNum = GetActorDailyDoneTimes( sysarg, enDailyOpTimes_DreamBoss, nil )
        --print(" dream boss, todayNum="..todayNum)
        if todayNum <= 0 then
            AddActorDailyDoneTimes( sysarg, enDailyOpTimes_DreamBoss, nil, 1 )
            local actorId = Actor.getActorId( sysarg )
            print(" dream boss, first enter, actorId="..actorId)
        end
    end

    local itemPtr = Item.getBagItemPtrById(sysarg, DreamLandBossConfig.DropItemId)
    if itemPtr then
        SetPropertyHeadItemValue(sysarg, 4, DreamLandBossConfig.DropItemId)  --增加头部item
    end
end

function DreamLandBossRelive(sysarg, reliveType)
    if not IsDreamLandBossStart() then
        return 
    end

    if not IsDreamLandBoss(sysarg) then
        return
    end
    if reliveType and reliveType == 2 then
        local itemPtr = Item.getBagItemPtrById(sysarg, DreamLandBossConfig.DropItemId)
        if itemPtr then
            SetPropertyHeadItemValue(sysarg, 4, DreamLandBossConfig.DropItemId)  --增加头部item
        end
        Actor.enterScene(sysarg,unpack(DreamLandBossConfig.EnterPos))
    end
end

--兑换活动开启
function DreamLandBossOpenNpcExchange()
    local sVar = System.getDyanmicVar()
    sVar.dreamLandBossExchangeStart = 1
end

--兑换活动时间内
function IsDreamLandBossExchangeStart()
    local sVar = System.getDyanmicVar()
    if sVar.dreamLandBossExchangeStart and sVar.dreamLandBossExchangeStart == 1 then
        return true
    end
    return false
end

--兑换
function DreamLandBossExchange(sysarg, type)
    if not IsDreamLandBossStart() then
        return 
    end

    if not IsDreamLandBoss(sysarg) then
        return
    end
    if not IsDreamLandBossExchangeStart() then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.DreamLandBoss021, ttFlyTip)
        return
    end
    if IsBagGridEnough(sysarg, DreamLandBossConfig.ExchangeItems[type].exchangeAwards) then
        local sVar = Actor.getStaticVar(sysarg)
        if not sVar.days then sVar.days = {} end
        if sVar.days.dreamLandBossExchangeCount and sVar.days.dreamLandBossExchangeCount[type] and sVar.days.dreamLandBossExchangeCount[type] >= DreamLandBossConfig.ExchangeItems[type].ExchangeCount then
            Actor.sendTipmsg(sysarg, Lang.ScriptTips.DreamLandBoss016, ttFlyTip)
            return false
        end
        if not CheckConsumeCond( sysarg, DreamLandBossConfig.ExchangeItems[type].costItems, 1 ) then
            --Actor.sendTipmsg(sysarg, Lang.ScriptTips.coin00001, ttFlyTip)
            return false
        end

        if not DoConsumeCond( sysarg, DreamLandBossConfig.ExchangeItems[type].costItems, 1, LogIds.DreamLandBossLogId001, Lang.LogTips.DreamLandBoss001 ) then
            return false
        end

        if not sVar.days.dreamLandBossExchangeCount then
            sVar.days.dreamLandBossExchangeCount = {}
        end
        if not sVar.days.dreamLandBossExchangeCount[type] then
            sVar.days.dreamLandBossExchangeCount[type] = 0
        end
        sVar.days.dreamLandBossExchangeCount[type] = sVar.days.dreamLandBossExchangeCount[type] + 1

        local logId = LogIds.DreamLandBossLogId002
        local logStr = Lang.LogTips.DreamLandBoss002
        GiveCommonAward(sysarg, DreamLandBossConfig.ExchangeItems[type].exchangeAwards, logId, logStr)


        local itemPtr = Item.getBagItemPtrById(sysarg, DreamLandBossConfig.DropItemId)
        if not itemPtr then
            SetPropertyHeadItemValue(sysarg, 4, 0)  --增加头部item
        end

        return true
    else
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
    end

    return false
end

--开始创建boss
function DreamLandBossCreateBoss() 
    local  day = System.getDaysSinceOpenServer()
    for idx, BossLst in ipairs(DreamLandBossConfig.BossList) do
        local BossInfo = GetElemByCond(BossLst, day)
		if BossInfo then
			DreamLandBossCreateOneBoss(idx, BossInfo)
		end
    end

    SendInfoToAllPlayer()

    Actor.unregScriptCallback( System.getGlobalNpc(), "DreamLandBossPreSecondTimeFunc")
    Actor.regScriptTimer(System.getGlobalNpc(), 0, 1000, 1000, DreamLandBossConfig.ActivityTime*1000, "DreamLandBossPreSecondTimeFunc")
end

--创建一个boss
function DreamLandBossCreateOneBoss(idx, BossInfo)
    local  dVar = System.getDyanmicVar()
    local  hScene = Fuben.getSceneHandleById(BossInfo.sceneId, 0)
    if hScene then
        if Fuben.getLiveMonsterCount(hScene, BossInfo.monsterId) > 0 then
            return
        end
        local  monster = Fuben.createMonster(hScene,BossInfo.monsterId,BossInfo.pos[1],BossInfo.pos[2],BossInfo.livetime)
        if monster == nil then
            return
        end
        if not dVar.DreamLandBossId then
            dVar.DreamLandBossId = {}
        end
        dVar.DreamLandBossId[idx] = BossInfo.monsterId
    end 
end

--创建一个死亡怪
function DreamLandBossCreateOneDeadMonster(idx, BossInfo)
    local  dVar = System.getDyanmicVar()
    local  hScene = Fuben.getSceneHandleById(BossInfo.sceneId, 0)
    if hScene then
        if Fuben.getLiveMonsterCount(hScene, BossInfo.deadMonsterId) > 0 then
            return
        end
        local  monster = Fuben.createMonster(hScene,BossInfo.deadMonsterId,BossInfo.pos[1],BossInfo.pos[2],BossInfo.revivalCd)
        if monster == nil then
            return
        end
        if not dVar.DreamLandBossDeadId then
            dVar.DreamLandBossDeadId = {}
        end
        dVar.DreamLandBossDeadId[idx] = BossInfo.deadMonsterId
        if not dVar.DreamLandBossDeadEndTime then
            dVar.DreamLandBossDeadEndTime = {}
        end
        dVar.DreamLandBossDeadEndTime[idx] = System.getCurrMiniTime() + BossInfo.revivalCd

        SendChangeInfoToAllPlayer(idx)
    end 
end

--定时器方法
function DreamLandBossPreSecondTimeFunc()
    local  dVar = System.getDyanmicVar()
    if not dVar.DreamLandBossDeadEndTime then
        return
    end
    local curTime = System.getCurrMiniTime()

    local sceneId = DreamLandBossConfig.BossScene
    local  hScene = Fuben.getSceneHandleById(sceneId, 0)
    for idx = 1, #DreamLandBossConfig.BossList do
        if dVar.DreamLandBossDeadEndTime[idx] and dVar.DreamLandBossDeadEndTime[idx] <= curTime then
            if dVar.DreamLandBossDeadId and dVar.DreamLandBossDeadId[idx] then
                Fuben.clearMonster(hScene, dVar.DreamLandBossDeadId[idx])
                local BossInfo = DreamLandBossGetBossInfoByDeadMonId(dVar.DreamLandBossDeadId[idx])
                if BossInfo then
                    DreamLandBossCreateOneBoss(idx, BossInfo)

                    SendChangeInfoToAllPlayer(idx)
                end
                dVar.DreamLandBossDeadId[idx] = nil
            end
            dVar.DreamLandBossDeadEndTime[idx] = nil
        end
    end
end

--结束活动踢出玩家
function DreamLandBossKickPlayers() 
    for ii, nSceneId in ipairs({DreamLandBossConfig.BossScene, DreamLandBossConfig.EnterPos[1]}) do
        local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表    
        if playerList then
           for k,v in pairs(playerList)do  --踢出场景
                Actor.enterScene(v,unpack(DreamLandBossConfig.ExitPos))   
                CloseCountDownPanel( v )
                CloseBoardPanel( v )

                SetPropertyHeadItemValue(sysarg, 4, 0)
                ClearBagItem(sysarg, DreamLandBossConfig.DropItemId)
            end 
        end
    end
end

--结束活动清除boss
function DreamLandBossClearBoss()
    local sceneId = DreamLandBossConfig.BossScene
    local  hScene = Fuben.getSceneHandleById(sceneId, 0)
    local  day = System.getDaysSinceOpenServer()
    for ii, BossLst in ipairs(DreamLandBossConfig.BossList) do
        local BossInfo = GetElemByCond(BossLst, day)
        if BossInfo then
			Fuben.clearMonster(hScene, BossInfo.monsterId)
			Fuben.clearMonster(hScene, BossInfo.deadMonsterId)
        end
    end
end

--怪物死亡
function OnDreamLandBossMonDie(monster, sysarg, monId)
    if not IsDreamLandBossStart() then
        return 
    end
    if not IsDreamLandBoss(sysarg) then
        return
    end
    local sceneId = Actor.getSceneId(sysarg)
    if sceneId ~= DreamLandBossConfig.BossScene then
        return
    end
    local  dVar = System.getDyanmicVar()
    if not dVar.DreamLandBossId then
        return
    end
    local state = false
    local monIdx
    for idx = 1, #DreamLandBossConfig.BossList do
        if dVar.DreamLandBossId[idx] and dVar.DreamLandBossId[idx] == monId then
            monIdx = idx
            state = true
            break
        end
    end
    if state == false then
        return
    end
    dVar.DreamLandBossId[monIdx] = nil
    
    local BossInfo = DreamLandBossGetBossInfoByMonId(monId)

    if not BossInfo then return end
    DreamLandBossCreateOneDeadMonster(monIdx, BossInfo)
end

--获取配置根据bossid
function DreamLandBossGetBossInfoByMonId(monId)
    local  day = System.getDaysSinceOpenServer()
    for ii, BossLst in ipairs(DreamLandBossConfig.BossList) do
        local BossInfo = GetElemByCond(BossLst, day)
        if BossInfo and BossInfo.monsterId == monId then
            return BossInfo
        end
    end
    return nil
end

--获取配置根据死亡怪物id
function DreamLandBossGetBossInfoByDeadMonId(monId)
    local  day = System.getDaysSinceOpenServer()
    for ii, BossLst in ipairs(DreamLandBossConfig.BossList) do
        local BossInfo = GetElemByCond(BossLst, day)
        if BossInfo and BossInfo.deadMonsterId == monId then
            return BossInfo
        end
    end
    return nil
end

--活动判断
function IsDreamLandBoss(sysarg)
    local sceneId = Actor.getSceneId(sysarg)
    if sceneId == DreamLandBossConfig.EnterPos[1] or sceneId == DreamLandBossConfig.BossScene then
        return true
    end
    return false
end

function IsDreamLandBossStart()
    local sVar = System.getDyanmicVar()
    if sVar.dreamLandBossStart and sVar.dreamLandBossStart == 1 then
        return true
    end
    return false
end

--当玩家离开场景
function onDreamLandBossEnterScene(sysarg, sceneId, lastSceneId)
    if sceneId and lastSceneId and sceneId == lastSceneId then
        return 
    end
    if sceneId == DreamLandBossConfig.BossScene then
        SendInfoToPlayer(sysarg)
    end
    if lastSceneId and lastSceneId == DreamLandBossConfig.BossScene then
        --SetPropertyHeadItemValue(sysarg, 4, 0)  --离开活动删除头部item
        CloseCountDownPanel( sysarg )
    end

    if sceneId ~= DreamLandBossConfig.BossScene and sceneId ~= DreamLandBossConfig.EnterPos[1] and 
        (lastSceneId == DreamLandBossConfig.BossScene or lastSceneId == DreamLandBossConfig.EnterPos[1]) then
        SetPropertyHeadItemValue(sysarg, 4, 0)
        ClearBagItem(sysarg, DreamLandBossConfig.DropItemId)
        --CloseCountDownPanel( sysarg )
        CloseBoardPanel( sysarg )
    end
end

--玩家下线
function DreamLandBossLogoutHandler(sysarg)
    DreamLandBossLeaveActivity(sysarg)
end

--离开活动场景
function DreamLandBossLeaveActivity(sysarg)
    --CloseActivityRightPanel(sysarg) 
    SetPropertyHeadItemValue(sysarg, 4, 0)  --离开活动删除头部item
    Actor.enterScene(sysarg,unpack(DreamLandBossConfig.ExitPos)) 
    ClearBagItem(sysarg, DreamLandBossConfig.DropItemId)
    CloseCountDownPanel( sysarg ) 
    CloseBoardPanel( sysarg )
end

--失去物品
function DreamLandBossDropItem( sysarg, itemId, itemCount )
    if not IsDreamLandBossStart() then
        return 
    end

    if not IsDreamLandBoss(sysarg) then
        return
    end
    --local num = Actor.getItemCount(sysarg, DreamLandBossConfig.DropItemId) 
    if itemCount > 0 then
        local num = Actor.getItemCount(sysarg, DreamLandBossConfig.DropItemId)
        if num > 0 then
            return
        end
        SetPropertyHeadItemValue(sysarg, 4, 0)  --删除头部item
    end
end

--获得物品
function DreamLandBossPickItem( sysarg, itemId, itemCount )
    if IsDreamLandBoss(sysarg) and IsDreamLandBossStart() then
        --local itemPtr = Item.getBagItemPtrById(sysarg, DreamLandBossConfig.DropItemId)
        --local num = Actor.getItemCount(sysarg, DreamLandBossConfig.DropItemId) 
        if itemCount > 0 then
            SetPropertyHeadItemValue(sysarg, 4, itemId)  --增加头部item
        end
    end
end

--全场景数据
function SendInfoToAllPlayer()
    local nSceneId = DreamLandBossConfig.BossScene
    local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表    
    if(playerList == nil)then
        return
    end
    for k,v in pairs(playerList)do  --踢出场景
        SendInfoToPlayer(v)
    end
end

--全场景数据更新
function SendChangeInfoToAllPlayer(idx)
    local nSceneId = DreamLandBossConfig.BossScene
    local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表    
    if(playerList == nil)then
        return
    end
    for k,v in pairs(playerList)do  --踢出场景
        SendChangeInfoToPlayer(v, idx)
    end
end

--个人数据
function SendInfoToPlayer(sysarg)
    if not IsDreamLandBossStart() then
        return 
    end

    if not IsDreamLandBoss(sysarg) then
        return
    end
    local panelInfo = {}
    local  dVar = System.getDyanmicVar()
    panelInfo.title = Lang.ScriptTips.DreamLandBoss017
    panelInfo.content = {}
    local  day = System.getDaysSinceOpenServer()
    for idx, BossLst in ipairs(DreamLandBossConfig.BossList) do
        local BossInfo = GetElemByCond(BossLst, day)
        if BossInfo then
            local info = {}
            info[1] = System.getMonsterNameById(BossInfo.monsterId)
            if dVar.DreamLandBossDeadEndTime and dVar.DreamLandBossDeadEndTime[idx] then
                info[2] = dVar.DreamLandBossDeadEndTime[idx] - System.getCurrMiniTime()
                if info[2] < 0 then
                    info[2] = 0
                end
            else
                info[2] = 0
            end
            info[3] = BossInfo.monsterId
            table.insert(panelInfo.content, info)
        end
    end
    OpenCountDownPanel( sysarg, ACTIVITY_DEFINE_DREAMLANDBOSS, panelInfo )
end

--个人数据更新
function SendChangeInfoToPlayer(sysarg, idx)
    if not IsDreamLandBossStart() then
        return 
    end

    if not IsDreamLandBoss(sysarg) then
        return
    end
    local content = {}
    content.idx = idx
    content.info = {}
    local  dVar = System.getDyanmicVar()
    local  day = System.getDaysSinceOpenServer()
    for ii, BossLst in ipairs(DreamLandBossConfig.BossList) do
        if ii == idx then
			local BossInfo = GetElemByCond(BossLst, day)
            if BossInfo then
                content.info[1] = System.getMonsterNameById(BossInfo.monsterId)
                if dVar.DreamLandBossDeadEndTime and dVar.DreamLandBossDeadEndTime[idx] then
                    content.info[2] = dVar.DreamLandBossDeadEndTime[idx] - System.getCurrMiniTime()
                    if content.info[2] < 0 then
                        content.info[2] = 0
                    end
                else
                    content.info[2] = 0
                end
                content.info[3] = BossInfo.monsterId
            end
            break
        end
    end
    ChangeCountDownPanel( sysarg, content )
end


function ClearBagItem(sysarg, itemId)
    --RemoveActorItemInBag( sysarg, itemId, LogIds.DreamLandBossLogId003, Lang.LogTips.DreamLandBoss003 )
    --Actor.sendTipmsg(sysarg, Lang.ScriptTips.DreamLandBoss022, ttFlyTip)
end
